Add FileShare.Delete to SimpleFSDirectory and FSIndexOutput, #1283 - #1344
Merged
Conversation
… path (apache#1283) SimpleFSDirectory opened its read handles with FileShare.ReadWrite, diverging from NIOFSDirectory (ReadWrite | Delete). On Windows this caused File.Delete to throw a sharing violation while a reader held the file open; IndexFileDeleter swallows that and defers the delete to a retry list, so superseded index files accumulate on disk for the lifetime of a long-lived reader. Add FileShare.Delete to both SimpleFSDirectory read handles and to the shared FSIndexOutput write handle (used by all FSDirectory subclasses) so that every handle Lucene.NET opens on a file permits deletion. Windows requires all open handles to allow Delete share for a delete to succeed, making the guarantee unconditional and matching the POSIX delete-while-open semantics that IndexFileDeleter already assumes. This is a deliberate divergence from upstream Java, whose RandomAccessFile (used by SimpleFS for both read and write) omits FILE_SHARE_DELETE on Windows; only FileChannel (NIOFS/MMap) includes it. MMapDirectory's read handle is addressed separately in apache#1267. Adds Windows-gated regression tests in TestDirectory that confirm a delete succeeds while a read or write handle is open (and that the still-open read handle keeps returning the original bytes). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
NightOwl888
approved these changes
Jun 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add FileShare.Delete to SimpleFSDirectory and FSIndexOutput
Fixes #1283
Description
SimpleFSDirectory opened its read handles with FileShare.ReadWrite, diverging from NIOFSDirectory (ReadWrite | Delete). On Windows this caused File.Delete to throw a sharing violation while a reader held the file open; IndexFileDeleter swallows that and defers the delete to a retry list, so superseded index files accumulate on disk for the lifetime of a long-lived reader.
Add FileShare.Delete to both SimpleFSDirectory read handles and to the shared FSIndexOutput write handle (used by all FSDirectory subclasses) so that every handle Lucene.NET opens on a file permits deletion. Windows requires all open handles to allow Delete share for a delete to succeed, making the guarantee unconditional and matching the POSIX delete-while-open semantics that IndexFileDeleter already assumes.
This is a deliberate divergence from upstream Java, whose RandomAccessFile (used by SimpleFS for both read and write) omits FILE_SHARE_DELETE on Windows; only FileChannel (NIOFS/MMap) includes it. MMapDirectory's read handle is addressed separately in #1267.
Adds Windows-gated regression tests in TestDirectory that confirm a delete succeeds while a read or write handle is open (and that the still-open read handle keeps returning the original bytes).